package com.egene.tw.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.egene.tw.R;
import com.egene.tw.activity.login.LoginActivity_;
import com.egene.tw.base.BaseActivity;
import com.viewpagerindicator.CirclePageIndicator;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_guide)
public class GuideActivity extends BaseActivity {
    int[] img = new int[]{R.drawable.guide_1,R.drawable.guide_2};

    @ViewById
    ViewPager vpGuide;
    @ViewById
    CirclePageIndicator indicator;
    @ViewById
    ImageView ivStartApp;

    @AfterViews
    void initViews(){
        vpGuide.setAdapter(new GuideAdapter(img));
        indicator.setViewPager(vpGuide);
        vpGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            @Override
            public void onPageSelected(int position) {
                if(position == img.length){
                    ivStartApp.setVisibility(View.VISIBLE);
                }else {
                    ivStartApp.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Click(R.id.ivStartApp)
    void startApp(){
        Intent intent = new Intent(this, LoginActivity_.class);
        startActivity(intent);
        finish();
    }

    /**
     * 引导页适配器
     */
    private class GuideAdapter extends PagerAdapter{

        private int[] mData;

        public GuideAdapter(int[] data){
            this.mData = data;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view;
            if(position ==  mData.length){
                view = View.inflate(container.getContext(), R.layout.guide_last, null);
                ImageView ivPlayVideo = (ImageView) view.findViewById(R.id.ivPlayVideo);
                ivPlayVideo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        MToast.show("play Video!");
                    }
                });
            }else{
                ImageView iv = new ImageView(GuideActivity.this);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                iv.setImageResource(mData[position]);
//                ImageUtil.loadImage(GuideActivity.this, iv, mData[position]);
                view = iv;
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return mData.length + 1;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
